package io.lkt.modules.sys.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import io.lkt.common.validator.ValidatorUtils;
import io.lkt.modules.sys.entity.BMessageLinkEntity;
import io.lkt.modules.sys.entity.SysUserEntity;
import io.lkt.modules.sys.service.BMessageLinkService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.lkt.modules.sys.entity.BMessageDictEntity;
import io.lkt.modules.sys.service.BMessageDictService;
import io.lkt.common.utils.PageUtils;
import io.lkt.common.utils.R;



/**
 * 微信消息推送占位符表
 *
 * @author xss
 * @email dustwas@163.com
 * @date 2020-12-15 17:15:18
 */
@RestController
@RequestMapping("/sys/bmessagedict")
public class BMessageDictController extends AbstractController {
    @Autowired
    private BMessageDictService bMessageDictService;

    @Autowired
    private BMessageLinkService bMessageLinkService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:bmessagedict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = bMessageDictService.queryPage(params);
        List<?> list = page.getList();
        if (CollectionUtils.isNotEmpty(list)) {
            List<BMessageDictEntity> bMessageDictList = Lists.newArrayList();

            for (Object temp : list) {
                BMessageDictEntity bMessageDictEntity = (BMessageDictEntity) temp;

                String linkName = "";

                if (bMessageDictEntity.getDictLink() != null) {
                    BMessageLinkEntity bMessageLinkTemp = bMessageLinkService.getById(bMessageDictEntity.getDictLink());

                    if(bMessageLinkTemp != null){
                        linkName = bMessageLinkTemp.getLinkName();
                    }
                }
                bMessageDictEntity.setDictLinkName(linkName);

                bMessageDictList.add(bMessageDictEntity);
            }
            page.setList(bMessageDictList);
        }

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:bmessagedict:info")
    public R info(@PathVariable("id") Integer id){
        BMessageDictEntity bMessageDict = bMessageDictService.getById(id);

        return R.ok().put("bMessageDict", bMessageDict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:bmessagedict:save")
    public R save(@RequestBody BMessageDictEntity bMessageDict){

        //获取当前登录用户
        SysUserEntity curUser = getUser();

        bMessageDict.setCreateBy(curUser.getUsername());
        bMessageDict.setCreateTime(new Date());
        bMessageDict.setUpdateBy(curUser.getUsername());
        bMessageDict.setUpdateTime(new Date());

        bMessageDictService.save(bMessageDict);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:bmessagedict:update")
    public R update(@RequestBody BMessageDictEntity bMessageDict){
        ValidatorUtils.validateEntity(bMessageDict);

        //获取当前登录用户
        SysUserEntity curUser = getUser();

        bMessageDict.setUpdateBy(curUser.getUsername());
        bMessageDict.setUpdateTime(new Date());
        bMessageDictService.updateById(bMessageDict);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:bmessagedict:delete")
    public R delete(@RequestBody Integer[] ids){
        bMessageDictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/queryBMessageLinkList")
    public R queryAll(){

        List<BMessageLinkEntity> bMessageLinkList = bMessageLinkService.queryAll();

        return R.ok().put("bMessageLinkList", bMessageLinkList);
    }

}
